/**
 * 
 */
package org.hotpotmaterial.codegenn2.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hotpotmaterial.codegenn2.dto.ResultOfTestDTO;
import org.hotpotmaterial.codegenn2.template.CodeGenerateExcutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import io.swagger.annotations.Api;

/**
 * @author cakydeveloper
 *
 */
@Controller
@RequestMapping("/hmteam/codegen/v1/test")
@Api(value = "测试Controller", tags={"测试"})
public class TestController {

	@Autowired
	private ApplicationContext applicationContext;

	@GetMapping("/allbeans")
	public ResponseEntity<ResultOfTestDTO> getAllBeans() {
		ResultOfTestDTO result = new ResultOfTestDTO();
		List<String> data = new ArrayList<>();
		data = Arrays.asList(applicationContext.getBeanDefinitionNames());
		result.setData(data);
		return new ResponseEntity<>(result, HttpStatus.OK);
	}

	@GetMapping("/plugin/demo/excute")
	public ResponseEntity<ResultOfTestDTO> getPluginDemoExcute(@RequestParam String pluginName,
			@RequestParam String pjname, @RequestParam String dbname) {
		ResultOfTestDTO result = new ResultOfTestDTO();
		CodeGenerateExcutor excutor = (CodeGenerateExcutor) applicationContext
				.getBean(pluginName);
		// 执行内容
		Map<String, String> renderParams = new HashMap<>();
		renderParams.put("pjname", pjname);
		renderParams.put("dbname", dbname);
		excutor.codeGenerateExcuting("unittestunittest", renderParams);
		result.setMsg(excutor != null ? excutor.toString() : "null");
		return new ResponseEntity<>(result, HttpStatus.OK);
	}

	@GetMapping("/plugins/properties")
	public ResponseEntity<ResultOfTestDTO> getPluginsProperties() {
		ResultOfTestDTO result = new ResultOfTestDTO();
		Map<String, Map<String, String>> mapData = new HashMap<>();
		Map<String, CodeGenerateExcutor> resultMap = applicationContext.getBeansOfType(CodeGenerateExcutor.class);
		resultMap.forEach((key, value) -> {
			mapData.put(key, value.getProperties());
		});
		result.setMapData(mapData);
		return new ResponseEntity<>(result, HttpStatus.OK);
	}

}
